const Net = require('../../common.js')
Page({
  data: {
    typelist: [],
    info: [],
    nowPage: 1,
    pageTapId: -1
  },
  onLoad: function (options) {
    var _this = this
    _this.onPageTap()
  },
  onPageTap() {
    var _this = this
    Net.link('v1/orderGroup', {
      now_page: 1
    }, function (res) {
      Net.hid()
      if (res.data.code == 6 || res.data.mag == "Token验证失败") {
        Net.filaModal()
      }
      console.log(res)
      for (let i in res.data.data.list) {
        if (i == 0) {
          res.data.data.list[i].click = true
        } else {
          res.data.data.list[i].click = false
        }
      }
      Net.data({
        typelist: res.data.data.list
      })
      _this.onPageInfo(res.data.data.list[0].status)
    }, 'GET')
  },
  onPageInfo(e) {
    Net.link('v1/mallOrder', {
      now_page: Net.data('nowPage'),
      status: e
    }, function (res) {
      console.log(res)
      if (Net.data('nowPage') > 1) {
        if (res.data.data.list.length == 0) {
          Net.toast('已加载全部数据', 'none')
          return false
        }
        Net.hid()
        var list = Net.data('info')
        for (let i in res.data.data.list) {
          list.push(res.data.data.list[i])
        }
        Net.data({
          info: list
        })
        return false
      } else {
        Net.data({
          info: res.data.data.list
        })
      }
    }, 'GET')
  },
  onOrderInfo(e) {
    var ordernum = e.currentTarget.dataset.ordernum
    Net.PageRoter('_onOrderInfo','?id='+ordernum,'nav')
  },
  clickTab(e) {
    Net.toast('加载中...', 'loading')
    var _this = this,
      id = e.currentTarget.id,
      list = _this.data.typelist
    for (let i in list) {
      if (id == list[i].status) {
        list[i].click = true
      } else {
        list[i].click = false
      }
    }
    Net.data({
      typelist: list,
      pageTapId: id,
      nowPage: 1
    })
    Net.hid()
    _this.onPageInfo(id)
  },
  onToDeliverGoods(e) {
    if (e.currentTarget.id == 1) {
      Net.PageRoter('_onToDeliverGoods', '?order_num=' + e.currentTarget.dataset.order_num, 'nav')
    } else if (e.currentTarget.id == 2) {
      Net.PageRoter('_address', '?id=' + e.currentTarget.dataset.order_num, 'nav')
    }
  },
  onReady: function () {},
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {
    Net.toast('加载中', 'loading')
    Net.data({
      nowPage: Net.data('nowPage') + 1
    })
    setTimeout(() => {
      this.onPageInfo(Net.data('pageTapId'))
    }, 300);
  },
  onShareAppMessage: function () {}
})